// pages/pay/Code.js

const basePage = require('../../common/BasePage')
Page({
  behaviors: [basePage],

  /**
   * 页面的初始数据
   */
  data: {
    img: '',
    setId: '',
    storeSid: "",
    goodsOrderSn: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("code onload", options);
    if (options.sn) {
      this.request({
        url: this.data.host + '/v1/orders/' + options.sn + '/pay-code',
        method: "post"
      }, res => {
        console.log("options.sn", res);
        this.setData({
          img: res.data
        })
      }, data => {
        wx.showModal({
          showCancel: false,
          content: data.result || data.msg + "" || data.message + "" || JSON.stringify(data),
          success() {
            wx.navigateBack({
              delta: 1,
            })
          }
        })
      })
    } else {
      this.setData({
        img: wx.getStorageSync('sk_img')
      })
    }

    if (options.storeSid) { //商品扫码下单+扫码核销   查询支付状态
      this.data.storeSid = options.storeSid;
      this.data.goodsOrderSn = options.goodsOrderSn || options.sn;
      this.data.setId = setInterval(() => {
        this.req_status();
      }, 1500);
    }
  },
  req_status() {
    this.request({
      url: this.data.host + '/v1/orders/goods-pay-status',
      method: "post",
      data: {
        storeSid: this.data.storeSid,
        goodsOrderSn: this.data.goodsOrderSn
      }
    }, res => {
      // console.log("支付状态", res);
      if (res.data) {
        clearInterval(this.data.setId);
        wx.redirectTo({
          url: '/pages/store/index?list=1&storeSid=' + this.data.storeSid + "&status=7",
        })
      }
    }, data => {
      wx.showModal({
        showCancel: false,
        content: data.result || data.msg + "" || data.message + "" || JSON.stringify(data),
        success() {
          wx.navigateBack({
            delta: 1,

          })
        }
      })
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    if (this.data.setId) {
      clearInterval(this.data.setId)
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})